// 感染模式插件

string author = "Live";
string pluginName = "infectMode 感染模式插件";
string version = "0.1";

bool gameStart = false;

// 插件初始化
void OnPluginInit()
{
	// 设置插件信息
	PluginData::SetAuthor(author);
	PluginData::SetName(pluginName);
	PluginData::SetVersion(version);

	// 提示
	ThePresident.InfoFeed("插件:"+pluginName+"| 版本:"+version+"| 作者:"+author,false);
	
	Engine.EnableCustomSettings(true);

    // 当没有存活的玩家时
    Events::ThePresident::OnNoSurvivorsRemaining.Hook( @OnNoSurvivorsRemaining );

    // 当玩家即将受到伤害
    Events::Player::OnPlayerDamagedPre.Hook( @OnPlayerDamagedPre );

	// 当玩家已经受到伤害
    Events::Player::OnPlayerDamaged.Hook( @OnPlayerDamaged );

	// 当僵尸受到伤害
    Events::Infected::OnInfectedDamaged.Hook( @OnInfectedDamaged );
	
	// 当玩家说
	Events::Player::PlayerSay.Hook( @PlayerSay );

    ThePresident_OnMapStart();
       
}

// 当幸存者玩家全部死亡
HookReturnCode OnNoSurvivorsRemaining( int iCandidate )
{
    // 匹配游戏模式，阻断默认的游戏结束事件（测试用）
	// If objective, then no game over
	if ( iCandidate == 1 ) return HOOK_HANDLED;
    // if ( iCandidate == GM_FLATLINE ) return HOOK_HANDLED;
	// if ( iCandidate == GM_PANIC ) return HOOK_HANDLED;
	// if ( iCandidate == GM_EXTRACTION ) return HOOK_HANDLED;
	return HOOK_CONTINUE;
}

void ThePresident_OnMapStart()
{
	ThePresident_OnRoundStart();
}

void ThePresident_OnRoundStart()
{

    // 设置感染者血量
    CASConVarRef@ z_player_health = ConVar::Find( "z_player_health" );
    z_player_health.SetValue("1000");

    // 禁用僵尸生成
    CASConVarRef@ thepresident_refuse_zombiespawn = ConVar::Find( "thepresident_refuse_zombiespawn" );
    thepresident_refuse_zombiespawn.SetValue("1");

    // 反复读取当前玩家人数
    // getPlayerNum();

}

void getPlayerNum(){
    int playerNum = Utils.GetNumPlayers(true);
    Chat.PrintToChat(all,"当前玩家人数为{hotpink}："+playerNum+"{white},游戏足够两人后开始游戏！");
    if(playerNum>=1){
        // 几秒后游戏开始
        fun_gameStart();
    }else{
        Schedule::Task(5.0,getPlayerNum);
    }
}

int time = 5;

void fun_gameStart(){
    Chat.PrintToChat(all,"游戏将在"+time+"秒后开始");
    time--;
    if(time==0){
        // 游戏开始
        gameStart = true;
        // 选择变异母体
        CTerrorPlayer@ z_player = GetRandomPlayer(false,true);
        z_player.ChangeTeam( TEAM_ZOMBIE );
        z_player.Respawn();
        Chat.PrintToChat(all,""+z_player.GetPlayerName()+"已被选为母体！");
        time = 5;
        Utils.ShowFakeTime(300,FL_BLINK_RED);
    }else{
        Schedule::Task(1.0f,fun_gameStart);
    }
}

HookReturnCode OnPlayerDamagedPre(CTerrorPlayer@ pPlayer, CTakeDamageInfo &in DamageInfo)
{
    // Chat.PrintToChat(pPlayer,pPlayer.GetPlayerName()+"预处理----受到伤害类型："+DamageInfo.GetDamageType());
	// Chat.PrintToChat(pPlayer,pPlayer.GetPlayerName()+"受到伤害类型："+DamageInfo.GetDamageType());
	// Chat.PrintToChat(all,""+DamageInfo.HasDamageType(DMG_BULLET));
	// 如果被枪打的伤害超过100（被爆头），设置100伤害
	if( DamageInfo.GetDamage() > 100.0f && DamageInfo.HasDamageType(DMG_BULLET))
		DamageInfo.SetDamage(100.0f);
	Chat.PrintToChat(pPlayer,pPlayer.GetPlayerName()+"受到伤害量："+DamageInfo.GetDamage());
    // DamageInfo.SetDamageType( DMG_PARALYZE );
	return HOOK_CONTINUE;
}

// 当玩家受到伤害后
HookReturnCode OnPlayerDamaged(CTerrorPlayer@ pPlayer, CTakeDamageInfo &in DamageInfo)
{
	// 降低玩家的速度
	pPlayer.SetMaxSpeed(50.0f, true);
	Schedule::Task(2.0f,pPlayer.entindex(),defaultSpeed);
	Chat.PrintToChat(all,"受到伤害，移速降低");
    // Chat.PrintToChat(pPlayer,"最大速度"+pPlayer.GetMaxSpeed());
    // Chat.PrintToChat(pPlayer,pPlayer.GetPlayerName()+"受到伤害量："+DamageInfo.GetDamage());
    // Chat.PrintToChat(pPlayer,pPlayer.GetPlayerName()+"受到伤害类型："+DamageInfo.GetDamageType());
	return HOOK_CONTINUE;
}

HookReturnCode OnInfectedDamaged(Infected@ pInfected, CTakeDamageInfo &in DamageInfo)
{
    // Chat.PrintToChat(all,"感染者受到伤害类型："+DamageInfo.GetDamageType());
	return HOOK_CONTINUE;
}

// 恢复移速
void defaultSpeed(int playIndex){
	CBasePlayer@ BasePlayer = ToBasePlayer(playIndex);
	CTerrorPlayer@ pPlayer = ToTerrorPlayer( BasePlayer );
	pPlayer.SetMaxSpeed(0.0f, true);
}

HookReturnCode PlayerSay(CTerrorPlayer@ pPlayer, CASCommand@ pArgs)
{
	string arg = pArgs.Arg( 1 );
	string arg1 = "";
	string arg2 = "";

	// 切分字符串(这样就可以用空格传一个命令参数了)
	int subIndex = arg.findFirst(" ",0);
	if(not (subIndex < 0)){
		arg1 = arg.substr(0,subIndex);
		arg2 = arg.substr(subIndex+1,-1);
		//Chat.PrintToChat(all,""+arg1+"|"+arg2);
	}else{
		arg1 = arg;
	}
	
	if(arg1 == "!js"){
		if(arg2 != ""){
			pPlayer.SetCharacter(parseInt(arg2,10,0));
			Chat.PrintToChat(all,"重选角色");
		}else{
			Chat.PrintToChat(all,"角色:"+pPlayer.GetSurvivorID());
		}
	}else if(arg1=="!t"){
		Chat.PrintToChat(all,"类型:"+DMG_BULLET);
	}
	return HOOK_CONTINUE;
}